<?php
// include constants
include "./includes/vars.php";
include "./includes/containers.php";
$pathToRoot = "./"; 		// or "";

?>



<?php
// content php code goes here

$link = mysql_connect($dbhost, $dbuser, $dbpass)
or Die('Cannot connect to database '.$dbhost.'+'. $dbuser.'+'. $dbpass.'+'.mysql_error());

mysql_select_db($dbname)
  or Die("Database not found.");



//walid.elswaify
/*** Code goes here **/ 




   if ($_GET['act'] == "download"){

    //check for file and send it to browser
    $sql = "SELECT * FROM books WHERE B_ID = " . $_GET['BID'];
    if ($result = mysql_query($sql)){
    
        $row = mysql_fetch_row($result);
        $filepath = getcwd() . $row[2];
        


    // extract the file name
    $filearray = explode ('/', $filepath);
    $filename = "";
    foreach ($filearray as $temp){
        if($temp != $filename && trim($temp) != ""){
            $filename = $temp;

        }
    }
    // get the file extension
    


 $status = FALSE;
 if (!is_file($filepath) or connection_status()!=0) $status = FALSE;
if (is_file($filepath)){
 header("Content-type: application/octet-stream");
 header("Content-Disposition: inline; filename=\"".$filename."\"");
 header("Content-length: ".(string)(filesize($filepath)));
 header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
 header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
 header("Cache-Control: no-cache, must-revalidate");
 header("Pragma: no-cache");


 if ($file = fopen($filepath, 'rb')) {
   while(!feof($file) and (connection_status()==0)) {
     print(fread($file, 1024*8));
     flush();
   }
   $status = (connection_status()==0);
   fclose($file);
   


 }
 


 }

if ($status == FALSE) {
die ("file transfer failed");
// either the file transfer was incomplete
// or the file was not found
}
else {
// the download was a success
// log, or do whatever else
// add 1 to the download count
$newcount = $row[4] + 1;
$sql = "UPDATE books SET B_DOWNLOAD_COUNT = '" . $newcount . "' WHERE B_ID = " . $_GET['BID'];
print $sql;
$result = mysql_query($sql)
or Die("error updating count");

}
        
        
        
        
        
        
        
        
    
    }
    else{
        $content = "Could not get this book's information.";
    }

}
        
        
        		
?>

